#include "LayerContainerPack.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "deprecated/CCDeprecated.h"
#include "ui/UIHelper.h"
#include "base/CCConsole.h"
#include "base/ccMacros.h"

#include "logic/LogicData.h"
#include "logic/LLogicEntity.h"
#include "common/PIOStreamOperator.h"

USING_NS_CC;
using namespace ui;


CLayerContainerPack::CLayerContainerPack(void) : m_Pack(nullptr), m_ItemDetail(nullptr), m_EntityPack(CLogicData::GetInstance()->GetOneself()->GetContainerPack())
{
}


CLayerContainerPack::~CLayerContainerPack(void)
{
}

bool CLayerContainerPack::init() {
	// 1. super init first
	if ( !CLayerBase::init() )
	{
		return false;
	}

	m_Pack = CSLoader::createNode("Inventory.csb");
	setContentSize(m_Pack->getContentSize());

	ui::Button* tLoginB = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Pack, 567);
	tLoginB->addTouchEventListener(this, toucheventselector(CLayerContainerPack::onClosePack));

	m_Items.SetUpdateItemFunc(CC_CALLBACK_2(CLayerContainerPack::updateItem, this));
	m_Items.SetContainer(m_EntityPack);
	for (uint32_t i = 0; i != NodeCOUNT; ++i) {
		std::string tName("Image_13_");
		tName << i;
		auto tNode = ui::Helper::seekWidgetByName((cocos2d::ui::Widget*)m_Pack, tName);
		m_Items.PushItem(tNode);
		tNode->addTouchEventListener(this, toucheventselector(CLayerContainerPack::onShowItemDetail));
	}

	m_ItemDetail = CSLoader::createNode("ItemDetail.csb");
	m_ItemDetail->setPosition(350, 450);
	m_ItemDetail->setVisible(false);
	m_ItemDetailOp = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_ItemDetail, 86);
	m_ItemDetailOp->addTouchEventListener(this, toucheventselector(CLayerContainerPack::onOpGoods));

	addChild(m_Pack);
	addChild(m_ItemDetail);

	return true;
}

void CLayerContainerPack::onEnter() {
	CLayerBase::onEnter();
	UpdateContent();
}

/*
void CLayerContainerPack::onExit() {
	CLayerBase::onExit();
}

void CLayerContainerPack::update(float delta) {
	CLayerBase::update(delta);
}
*/

void  CLayerContainerPack::UpdateContent() {
	m_Items.UpdatePage(1);
}

void CLayerContainerPack::updateItem(cocos2d::Node* node, const CNodeContainerItemManager::ItemContent& content) {
	//CCLOG("pack|update|item|tag|%d|content|count|%d|pic|%s",node->getTag(), content.m_Count, content.m_BackPic);
}

void CLayerContainerPack::onShowItemDetail(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED: {
		cocos2d::Node* tItem = (cocos2d::Node*)pSender;
		CNodeContainerItemManager::ContainerItem* tItemContent = (CNodeContainerItemManager::ContainerItem*)tItem->getUserData();

		if (tItemContent->m_goods.m_Type <= GoodsStartType) {
			m_ItemDetail->setVisible(false);
			break;
		}
		m_ItemDetailOp->setUserData(tItemContent);

		ui::Text* tTextType = (ui::Text*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_ItemDetail, 132);
		ui::Text* tTextQuality = (ui::Text*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_ItemDetail, 133);
		ui::Text* tTextLevel = (ui::Text*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_ItemDetail, 135);
		ui::Text* tTextNumber = (ui::Text*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_ItemDetail, 134);
		ui::Text* tTextTrade = (ui::Text*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_ItemDetail, 136);
		ui::Text* tTextValue = (ui::Text*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_ItemDetail, 137);
		ui::Text* tTextId = (ui::Text*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_ItemDetail, 150);

		std::string tValue;
		tValue << tItemContent->m_goods.m_Type;
		tTextType->setString(tValue);

		tValue.clear();
		tValue << tItemContent->m_goods.m_Quality;
		tTextQuality->setString(tValue);

		tValue.clear();
		tValue << tItemContent->m_goods.m_Level;
		tTextLevel->setString(tValue);

		tValue.clear();
		tValue << tItemContent->m_goods.m_Number;
		tTextNumber->setString(tValue);

		tValue.clear();
		tValue << tItemContent->m_goods.m_TradeFlag;
		tTextTrade->setString(tValue);

		tValue.clear();
		tValue << tItemContent->m_goods.m_SurValue;
		tTextValue->setString(tValue);

		tValue.clear();
		tValue << tItemContent->m_goods.m_Id;
		tTextId->setString(tValue);
		m_ItemDetail->setVisible(true);
		} break;
	}
}

void CLayerContainerPack::onOpGoods(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED: {
		ui::Button* tItem = (ui::Button*)pSender;
		CNodeContainerItemManager::ContainerItem* tItemContent = (CNodeContainerItemManager::ContainerItem*)tItem->getUserData();
		CGLGoodsBase* tGoods = m_EntityPack->GetGoods(tItemContent->m_ContainerBox, tItemContent->m_ContainerIndex);
		if (nullptr != tGoods) {
			CTLLogicEntity tLogicEntity;
			tLogicEntity.OpenOneBox(tItemContent->m_ContainerBox, tItemContent->m_ContainerIndex, tGoods->GetGoodsInfo());
		}
		} break;
	}
}

void CLayerContainerPack::onClosePack(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:
		this->setVisible(false);
		break;
	}
}
